package com.aube.control;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import com.aube.R;
import com.huyn.baseframework.dynamicload.video.AbstractController;
import com.huyn.baseframework.model.ChannelDetail;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by huyaonan on 16/3/23.
 */
public class CardControllerDispatcher {

    private List<AbstractController> mControllerList;
    private AbstractController.IControllerCallback mCallback;
    private ChannelDetail mChannel;

    public CardControllerDispatcher(Context context, View mRoot, ChannelDetail mChannel, AbstractController.IControllerCallback callback) {
        mControllerList = new ArrayList<>();
        this.mCallback = callback;
        this.mChannel = mChannel;

        mControllerList.add(new MultiScreenController(context, (ViewGroup) mRoot.findViewById(R.id.transition_video), mChannel, callback));
    }

    public void addControllers(List<AbstractController> controllers) {
        if(controllers == null || mControllerList == null)
            return;
        for(AbstractController controller : controllers)
            controller.init(mChannel, mCallback);
        mControllerList.addAll(controllers);
    }

    public void initData(long selectedSecond, boolean freshData, boolean autoBegin) {
        if(mControllerList == null)
            return;

        for(int i=0; i<mControllerList.size(); i++) {
            AbstractController controller =  mControllerList.get(i);
            controller.setupCards(selectedSecond, freshData, autoBegin);
        }
    }

    public void reverseFromFullScreen() {
        for(int i=0; i<mControllerList.size(); i++) {
            AbstractController controller =  mControllerList.get(i);
            controller.reverseFromFullScreen();
        }
    }

    public boolean onBack() {
        if(mControllerList == null)
            return false;

        for(int i=0; i<mControllerList.size(); i++) {
            AbstractController controller =  mControllerList.get(i);
            if(controller.onBack())
                return true;
        }

        return false;
    }

    public void onDestroy() {
        if(mControllerList == null)
            return;

        for(int i=0; i<mControllerList.size(); i++) {
            AbstractController controller =  mControllerList.get(i);
            controller.onDestroy();
        }

        mControllerList.clear();
    }

    public void endPlay() {
        if(mControllerList == null)
            return;

        for(int i=0; i<mControllerList.size(); i++) {
            AbstractController controller =  mControllerList.get(i);
            controller.end();
        }
    }

    public void start() {
        if(mControllerList == null)
            return;

        for(int i=0; i<mControllerList.size(); i++) {
            AbstractController controller =  mControllerList.get(i);
            controller.start();
        }
    }

    public void pause() {
        if(mControllerList == null)
            return;

        for(int i=0; i<mControllerList.size(); i++) {
            AbstractController controller =  mControllerList.get(i);
            controller.pause();
        }
    }

    public void pauseWhenFreeze() {
        if(mControllerList == null)
            return;

        for(int i=0; i<mControllerList.size(); i++) {
            AbstractController controller =  mControllerList.get(i);
            controller.pauseWhenFreeze();
        }
    }

    public void seek(long target) {
        if(mControllerList == null)
            return;

        for(int i=0; i<mControllerList.size(); i++) {
            AbstractController controller =  mControllerList.get(i);
            controller.seek(target);
        }
    }


    public void loadCard(long current) {
        if(mControllerList == null)
            return;

        for(int i=0; i<mControllerList.size(); i++) {
            mControllerList.get(i).locateCardByTime(current);
        }
    }

}
